package cn.iocoder.yudao.module.system.controller.admin.gugu;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.MajorRecommendReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.MajorRecommendRespVO;
import cn.iocoder.yudao.module.system.service.gugu.MajorRecommendationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 智能推荐专业测试控制器
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 智能推荐专业测试")
@RestController
@RequestMapping("/system/major-recommendation-test")
@Validated
@Slf4j
public class MajorRecommendationTestController {

    @Resource
    private MajorRecommendationService majorRecommendationService;

    @PostMapping("/test-recommend")
    @Operation(summary = "测试智能推荐专业")
    public CommonResult<MajorRecommendRespVO> testRecommendMajors() {
        log.info("开始测试智能推荐专业接口");
        
        try {
            // 构造测试请求参数
            MajorRecommendReqVO reqVO = new MajorRecommendReqVO();
            reqVO.setPageNum(1);
            reqVO.setPageSize(10);
            reqVO.setYear(2024);
            reqVO.setProvince("江苏");
            reqVO.setScore(580);
            reqVO.setRanks(15000);
            reqVO.setSubjects("物理,化学,生物");
            reqVO.setClassify("物理");
            reqVO.setBatch("本科批");
            reqVO.setMinScore(570);
            reqVO.setMaxScore(590);
            reqVO.setMajorKeyword("计算机");
            reqVO.setDisciplinaryCategory("工学");
            
            // 设置偏好
            MajorRecommendReqVO.PreferencesVO preferences = new MajorRecommendReqVO.PreferencesVO();
            preferences.setRegions(Arrays.asList("江苏", "上海"));
            preferences.setSchoolTypes(Arrays.asList("985", "211"));
            preferences.setMajorCategories(Arrays.asList("工学"));
            preferences.setCareerDirections(Arrays.asList("软件开发", "人工智能"));
            preferences.setOnlyHotMajors(false);
            preferences.setPreferLocalSchools(true);
            reqVO.setPreferences(preferences);
            
            log.info("测试请求参数：{}", reqVO);
            
            // 调用推荐服务
            MajorRecommendRespVO result = majorRecommendationService.recommendMajors(reqVO);
            
            log.info("测试推荐结果：总数={}, 当前页数据量={}", result.getTotal(), result.getSize());
            
            return success(result);
        } catch (Exception e) {
            log.error("测试智能推荐专业失败", e);
            return CommonResult.error(500, "测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/test-probability")
    @Operation(summary = "测试计算专业录取概率")
    public CommonResult<Double> testCalculateProbability() {
        log.info("开始测试计算专业录取概率");
        
        try {
            Double probability = majorRecommendationService.calculateMajorAdmissionProbability(
                    15000, "uuid-001", "计算机科学与技术", "江苏", 2024, "物理");
            
            log.info("测试概率计算结果：{}", probability);
            
            return success(probability);
        } catch (Exception e) {
            log.error("测试计算专业录取概率失败", e);
            return CommonResult.error(500, "测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/test-type")
    @Operation(summary = "测试判断推荐类型")
    public CommonResult<String> testDetermineType() {
        log.info("开始测试判断推荐类型");
        
        try {
            String type1 = majorRecommendationService.determineRecommendationType(85.0);
            String type2 = majorRecommendationService.determineRecommendationType(65.0);
            String type3 = majorRecommendationService.determineRecommendationType(35.0);
            
            String result = String.format("85%%->%s, 65%%->%s, 35%%->%s", type1, type2, type3);
            log.info("测试推荐类型判断结果：{}", result);
            
            return success(result);
        } catch (Exception e) {
            log.error("测试判断推荐类型失败", e);
            return CommonResult.error(500, "测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/test-reason")
    @Operation(summary = "测试生成推荐理由")
    public CommonResult<String> testGenerateReason() {
        log.info("开始测试生成推荐理由");
        
        try {
            String reason = majorRecommendationService.generateMajorRecommendationReason(
                    "南京大学", "计算机科学与技术", 75.0, "WENTUO", 
                    Arrays.asList("热门专业", "就业前景好"));
            
            log.info("测试推荐理由生成结果：{}", reason);
            
            return success(reason);
        } catch (Exception e) {
            log.error("测试生成推荐理由失败", e);
            return CommonResult.error(500, "测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/test-rank")
    @Operation(summary = "测试根据分数计算位次")
    public CommonResult<Integer> testCalculateRank() {
        log.info("开始测试根据分数计算位次");
        
        try {
            Integer rank = majorRecommendationService.calculateRankByScore(580, "江苏", 2024, "物理");
            
            log.info("测试位次计算结果：{}", rank);
            
            return success(rank);
        } catch (Exception e) {
            log.error("测试根据分数计算位次失败", e);
            return CommonResult.error(500, "测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/test-major-tags")
    @Operation(summary = "测试获取专业标签")
    public CommonResult<java.util.List<String>> testGetMajorTags() {
        log.info("开始测试获取专业标签");
        
        try {
            java.util.List<String> tags = majorRecommendationService.getMajorTags("计算机科学与技术", "uuid-001");
            
            log.info("测试专业标签获取结果：{}", tags);
            
            return success(tags);
        } catch (Exception e) {
            log.error("测试获取专业标签失败", e);
            return CommonResult.error(500, "测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/test-school-tags")
    @Operation(summary = "测试获取学校标签")
    public CommonResult<java.util.List<String>> testGetSchoolTags() {
        log.info("开始测试获取学校标签");
        
        try {
            java.util.List<String> tags = majorRecommendationService.getSchoolTags("uuid-001");
            
            log.info("测试学校标签获取结果：{}", tags);
            
            return success(tags);
        } catch (Exception e) {
            log.error("测试获取学校标签失败", e);
            return CommonResult.error(500, "测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/test-filter-majors")
    @Operation(summary = "测试根据选科筛选专业")
    public CommonResult<java.util.List<String>> testFilterMajors() {
        log.info("开始测试根据选科筛选专业");
        
        try {
            java.util.List<String> subjects = Arrays.asList("物理", "化学", "生物");
            java.util.List<String> majors = majorRecommendationService.filterMajorsBySubjects(
                    subjects, "江苏", 2024, "物理", "本科批");
            
            log.info("测试专业筛选结果：{}", majors);
            
            return success(majors);
        } catch (Exception e) {
            log.error("测试根据选科筛选专业失败", e);
            return CommonResult.error(500, "测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/health")
    @Operation(summary = "健康检查")
    public CommonResult<String> health() {
        return success("智能推荐专业测试接口运行正常");
    }
}
